package com.personal.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 萌新
 * @version 1.0
 * @date 2021/4/14 11:39
 */

public class VerifyCodeUtil {

    private static final Logger logger = LoggerFactory.getLogger(VerifyCodeUtil.class);

    /**
     * 验证码校验
     * @param request
     * @return
     */
    public static boolean verifyCode(HttpServletRequest request) {
        // 图片中的验证码
        String verifyCodeExpected = (String) request.getSession().getAttribute(
                com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        logger.debug("verifyCodeExpected:{}", verifyCodeExpected);
        // 用户输入的验证码
        String verifyCodeActual = HttpServletRequestUtil.getString(request,
                "verifyCodeActual");
        logger.debug("verifyCodeActual:{}", verifyCodeActual);
        if (verifyCodeActual == null || !verifyCodeActual.equalsIgnoreCase(verifyCodeExpected)) {
            return false;
        }
        return true;
    }
}
